package com.resourcestore.action;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.DepartmentDao;
import com.resourcestore.model.Department;

public class DepartmentAction extends BaseAction {
	
	private Department department;
	private List<Department> listDepartment;
	private DepartmentDao departmentDao = new DepartmentDao();
	private Pagination pagination = new Pagination();

	
	/**
	 * 查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	
	public String listDepartment(){
		department = new Department();
		pagination.setTotal(departmentDao.queryDepartmentNum(department));
		listDepartment = departmentDao.selectDepartmentPage(department,pagination);
		
		//将bigint数字处理为时间格式
		for (int i = 0; i < listDepartment.size(); i++) {
				try {
					listDepartment.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listDepartment.get(i).getGmtCreated()));
					listDepartment.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listDepartment.get(i).getGmtModified()));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		}
		
		return "default";
	}
	
	
	/**
	 * 根据条件搜索，查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String searchListDepartment(){
		
		//对内容进行urldecoder解码
		try {
			department.setDepartmentName(new String(department.getDepartmentName().getBytes("iso-8859-1"),"UTF-8"));
			department.setDepartmentRemark(new String(department.getDepartmentRemark().getBytes("iso-8859-1"),"UTF-8"));
			department.setAuthor(new String(department.getAuthor().getBytes("iso-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		pagination.setTotal(departmentDao.queryDepartmentNum(department));
		listDepartment = departmentDao.selectDepartmentPage(department,pagination);
		
		//将bigint数字处理为时间格式
		for (int i = 0; i < listDepartment.size(); i++) {
			try {
			listDepartment.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listDepartment.get(i).getGmtCreated()));
			listDepartment.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listDepartment.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return newpath("/Department/listDepartment.jsp");
	}
	
	/**
	 * 预插入一条数据
	  */		
	public String preInsertDepartment(){
		
		return newpath("/Department/insertDepartment.jsp");		
	}
	/**
	 * 插入一条数据
	  */		
	public String insertDepartment(){
		
		try {
			department.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			department.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		departmentDao.insertDepartment(department);
		return redirect(this.getUrl());		
	}
	
	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateDepartment(){
		department = departmentDao.selectDepartmentById(department);
		
		return newpath("/Department/updateDepartment.jsp");
	}
	/**
	 * 更新信息
	 * 
	 */
	public String updateDepartment(){
		
		try {
			//department.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			department.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		departmentDao.updateDepartment(department);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteDepartment(){
		departmentDao.deleteDepartment(department);
		return redirect(this.getUrl());
	}
	
	/**
	 * 批量删除信息
	 */
	public String deleteMoreDepartment(){
	
		departmentDao.deleteMoreDepartment(department);
		return redirect(this.getUrl());
	}

	
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List<Department> getListDepartment() {
		return listDepartment;
	}

	public void setListDepartment(List<Department> listDepartment) {
		this.listDepartment = listDepartment;
	}
	
	 public Pagination getPagination() {
		return this.pagination;
	 }

	 public void setPagination(Pagination pagination) {
		 this.pagination = pagination;
	 }
}
